package com.wuxu.ads_no.domain.page

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.MotionEvent
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.wuxu.ads_no.domain.service.AdCheckService
import com.wuxu.ads_no.ui.theme.Ads_NoTheme
import com.wuxu.ads_no.util.isAccessibilitySettingsOn
import com.wuxu.ads_no.util.logD

class MainActivity : ComponentActivity() {
    private val mTag = MainActivity::class.java.simpleName
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Ads_NoTheme(){
                val owner = LocalLifecycleOwner.current

                //Compose 采用Flutter方式，由状态驱动数据变化。Activity的页面状态可以使用Lifecycle监听
                val lifeEvent : MutableState<Lifecycle.Event> = remember { mutableStateOf(Lifecycle.Event.ON_RESUME) }

                DisposableEffect(owner ){
                    val observer = LifecycleEventObserver{_,event->
                        lifeEvent.value =  event
                    }

                    owner.lifecycle.addObserver(observer)
                    onDispose { owner.lifecycle.removeObserver(observer) }
                }

                Surface(
                    modifier = Modifier.wrapContentWidth(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    if (lifeEvent.value == Lifecycle.Event.ON_RESUME){
                        CheckAccessibilityIsOpen()
                    }
                }
            }
        }

    }


    @Composable
    private fun CheckAccessibilityIsOpen(){
        val isOpen = this.isAccessibilitySettingsOn(AdCheckService::class.java)
        if (isOpen){
            startService(Intent(this, AdCheckService::class.java))
            mainUi2()
        }else{
            ShowDialog(){
                startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
            }
        }
//        startActivity(Intent(this, ConfigActivity::class.java))
    }

    @Composable
    private fun  mainUi2(){
        Box(
            contentAlignment= Alignment.Center,
            modifier= Modifier
                .size(200.dp, 200.dp)
                .background(color = Color.Red, shape = CircleShape)
                .clickable {
                    startActivity(Intent(this, ConfigActivity::class.java))
                }){
            Text(text = "跳转配置页面")
        }
    }

    @Composable
    private fun ShowDialog(click:()->Unit){
        Dialog(onDismissRequest = {}, properties = DialogProperties()) {
            Box(modifier = Modifier
                .background(Color.Transparent)
                .fillMaxHeight()
                .fillMaxWidth()){
                Button(onClick = { click() },
                    Modifier
                        .fillMaxWidth()
                        .height(100.dp)
                        .align(Alignment.BottomCenter)
                ) {
                    Text(text = "点击打开无障碍权限")
                }
            }
        }

    }

    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
            "x:${event?.rawX},y:${event?.rawY}"

        return super.dispatchTouchEvent(event)
    }
}


@Composable
private fun  mainUi(){
    Box(
        contentAlignment= Alignment.Center,
        modifier= Modifier
            .width(200.dp)
            .height(200.dp)
            .background(color = Color.Yellow, shape = CircleShape)
            .clickable {
            }){
        Text(text = "跳转配置页面")
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    Ads_NoTheme {
        Box(modifier = Modifier
            .background(Color(0x4D000000))
            .fillMaxHeight()
            .fillMaxWidth()){
            Button(onClick = { },
                Modifier
                    .fillMaxWidth()
                    .height(100.dp)
                    .align(Alignment.BottomCenter)
                ) {
                Text(text = "点击打开无障碍权限")
            }
        }
    }
}